package test;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import util.DruidUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;

public class SqlInit {

    public static String readSQL(String path) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
        StringBuilder sb = new StringBuilder();
        String str;
        while ((str = reader.readLine()) != null) {
            sb.append(str);

        }
        reader.close();
        return sb.toString();
    }

    @Test
    public void creatData() throws IOException, SQLException {
        String s = SqlInit.readSQL("src/sql/init.sql");
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        for (String sql : s.split(";")) {
            queryRunner.update(sql);
        }
    }

}
